package top.huntwolf.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.huntwolf.admin.common.JsonResult;
import top.huntwolf.admin.pojo.cmsUser.Route;
import top.huntwolf.admin.service.RouteService;

import java.util.List;


@CrossOrigin
@RestController
@RequestMapping("/cms/route")
@Api(description = "cms路由控制器")
public class CMSRouteController {

    @Autowired
    private RouteService routeService;

    @RequiresPermissions(value = "route:find:all")
    @GetMapping("/all")
    @ApiOperation("获取所有路由表信息")
    public JsonResult<List<Route>> searchAll(){
        return JsonResult.success(routeService.searchAllRoute());
    }


    @RequiresPermissions(value = "route:remove")
    @DeleteMapping("/{id}")
    @ApiOperation("删除对应id的信息")
    public JsonResult removeRoute(@PathVariable Integer id) {
        routeService.deleteRoute(id);
        return JsonResult.success(null);
    }

    @RequiresPermissions(value = "route:add")
    @PostMapping("")
    @ApiOperation("增加路由")
    public JsonResult addRoute(@RequestBody Route route) {
        routeService.insertRoute(route);
        return JsonResult.success(null);
    }

    @RequiresPermissions(value = "route:modify")
    @PutMapping("")
    @ApiOperation("删除路由")
    public JsonResult modifyRoute(@RequestBody Route route) {
        routeService.updateRoute(route);
        return JsonResult.success(null);
    }

    @GetMapping("")
    @ApiOperation("获取登录用户拥有的路由")
    public JsonResult<List<Route>> searchRouteTable(){
        return JsonResult.success(routeService.searchRouteTable());
    }

    @GetMapping("/id/{id}")
    @ApiOperation("根据id获取路由")
    public JsonResult<Route> searchRoute(@PathVariable Integer id){
        return JsonResult.success(routeService.searchRoute(id));
    }

    @GetMapping("/roleId/{roleId}")
    @ApiOperation("根据角色id获取路由")
    public JsonResult<List<Route>> searchRouteByRoleId(@PathVariable Integer roleId){
        return JsonResult.success(routeService.searchRouteByRoleId(roleId));
    }


}
